layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--产品管理
     */
    var Product = {
        tableId: "ProductTable",    //表格id
        condition: {
            productName: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Product.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id',align: "center", sort: true, title: 'id'},
            {field: 'name', align: "center", sort: true, title: '产品名称'},
            {field: 'productTypeName', align: "center", sort: true, title: '产品分类'},
            {field: 'length', align: "center", sort: true, title: '包装长度(mm)'},
            {field: 'width', align: "center", sort: true, title: '包装宽度(mm)'},
            {field: 'weight', align: "center", sort: true, title: '净重（g)'},
            {field: 'material', align: "center", sort: true, title: '材料'},
            {field: 'label', align: "center", sort: true, title: '标签'},
            {field: 'is_flag',hide: true, align: "center", sort: true, title: '上架/下架状态'},
            {field: 'productStatus', align: "center", sort: true, title: '上架/下架'},
            {field: 'tenant_id',hide: true, align: "center", sort: true, title: '租户ID'},
            {field: 'tenantName', align: "center", sort: true, title: '租户'},
            {field: 'ex_attr', align: "center", sort: true, title: '特殊属性'},
            {field: 'create_time', align: "center", sort: true, title: '创建时间'},
            {field: 'create_user_id', align: "center", sort: true, title: '创建人'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Product.search = function () {
        var queryData = {};
        queryData['productName'] = $("#productName").val();
        table.reload(Product.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加产品
     */
    Product.openAddProduct = function () {
        func.open({
            height: 470,
            title: '添加产品',
            content: Feng.ctxPath + '/product/product_add',
            tableId: Product.tableId
        });
    };

    /**
     * 点击编辑产品
     *
     * @param data 点击按钮时候的行数据
     */
    Product.onEditProduct = function (data) {
        func.open({
            height: 470,
            title: '修改产品',
            content: Feng.ctxPath + "/product/product_edit?productId=" + data.id,
            tableId: Product.tableId
        });
    };

    /**
     * 图片上传
     *
     * @param data 点击按钮时候的行数据
     */
    Product.onUpload = function (data) {
        func.open({
            height: 470,
            title: '上传产品图片',
            content: Feng.ctxPath + "/product/product_upload?productId=" + data.id,
            tableId: Product.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Product.exportExcel = function () {
        var checkRows = table.checkStatus(Product.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除产品
     *
     * @param data 点击按钮时候的行数据
     */
    Product.onDeleteProduct = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/product/remove/"+ data.id, function () {
                Feng.success("删除成功!");
                table.reload(Product.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除产品 " + data.name + "?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Product.tableId,
        url: Feng.ctxPath + '/product/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Product.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Product.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Product.openAddProduct();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Product.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Product.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Product.onEditProduct(data);
        } else if (layEvent === 'delete') {
            Product.onDeleteProduct(data);
        } else if (layEvent === 'upload') {
            Product.onUpload(data);
        }
    });
});
